package cn.zut.hhr.class1.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 
 * </p>
 *
 * @author hhr
 * @since 2021-04-29
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SysUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    private Integer uid;

    private String name;

    private String password;

    private String salt;

    private Integer state;

    private String username;

    private Boolean enabled;

    private String cnname;

    private List<SysRole> roles;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<SysRole> roles = this.getRoles();
        roles.forEach(sysRole -> authorities.add(new SimpleGrantedAuthority(sysRole.getRole())));
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
